﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listInterestGroupsOutput : Api_BaseOutput
   {
      public listInterestGroupsResults result { get; set; }
      public listInterestGroupsParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listInterestGroupsOutput( listInterestGroupsInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new listInterestGroupsResults();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Name: {0}, Groups: {1}", result.name, apiHelper.MakeStringFromList( this.result.groups ) );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_listInterestGroups rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            IEnumerable<XElement> entries = xml.Elements();
            listInterestGroupsResults results = new listInterestGroupsResults();
            foreach ( XElement entry in entries )
            {
               if ( entry.Name == "name" ) results.name = entry.Value;
               else if ( entry.Name == "form_field" ) results.form_field = entry.Value;
               else if ( entry.Name == "groups" )
               {
                  if ( entry.HasElements ) foreach ( XElement group in entry.Elements() )
                     {
                        results.groups.Add( group.Value );
                     }
               }
            }
            this.result = results;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listInterestGroups jsonResult = jss.Deserialize<x_listInterestGroups>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- FormatResults
      private listInterestGroupsResults FormatResults( x_listInterestGroups x_results )
      {
         listInterestGroupsResults results = new listInterestGroupsResults();
         //
         results.name = x_results.name;
         results.form_field = x_results.form_field;
         results.groups = apiHelper.MakeListFromArray<string>( x_results.groups );
         //
         return results;
      }

      #endregion
   }
}
